//
//  OAMessageDetailViewController.m
//  ERP
//
//  Created by goldeney on 15/3/17.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "OAMessageDetailViewController.h"
#import "NSDictionary+DataFormat.h"

#import "AutoTextTableViewCell.h"

@interface OAMessageDetailViewController ()

@property (nonatomic, strong) NSMutableArray *messageData;
@property (nonatomic, strong) NSString *messageContent;

@end

@implementation OAMessageDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTitle: @"消息明细"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 10)];
    
}

- (void)getPassingParameter:(id)object
{
    NSInteger state = [((NSArray*)object)[0] integerValue];
    NSDictionary *dic = ((NSArray*)object)[1];
    
    if (state == 0) { //我的消息
        self.messageContent = [dic objectFormatStringForKey:@"msgcontent"];
        NSMutableArray *messageArray = [NSMutableArray array];
        NSString *readState = [[dic objectFormatForKey:@""] integerValue] > 0 ? @"已读" : @"未读";
        
        [self addContractInfoKey:@"functionname" toKey:@"消息类型：" defaults:@"未知" format:nil form:dic toArray:messageArray];
        [self addContractInfoKey:@"username" toKey:@"发  送  人：" defaults:@"未知" format:nil form:dic toArray:messageArray];
        [self addContractInfoKey:@"creationtime" toKey:@"发送时间：" defaults:@"未知" format:nil form:dic toArray:messageArray];
        [self addContractInfoKey:nil toKey:@"阅读状态：" defaults:readState format:nil form:dic toArray:messageArray];
        self.messageData = messageArray;
        
    } else { //已发送消息
        self.messageContent = [dic objectFormatStringForKey:@"msgcontent"];
        NSMutableArray *messageArray = [NSMutableArray array];
        
        [self addContractInfoKey:@"" toKey:@"消息类型：" defaults:@"一般消息" format:nil form:dic toArray:messageArray];
        [self addContractInfoKey:@"senduserid" toKey:@"发  送  人：" defaults:@"未知" format:nil form:dic toArray:messageArray];
        [self addContractInfoKey:@"creationtime" toKey:@"发送时间：" defaults:@"未知" format:nil form:dic toArray:messageArray];
        self.messageData = messageArray;
    }
}

- (void)addContractInfoKey:(id)key toKey:(id)tokey defaults:(id)def format:(id)formatText
                      form:(NSDictionary*)dic toArray:(NSMutableArray*)array
{
    id value = [dic objectFormatForKey:key];
    if (!def) def = @"";
    
    if (!value){
        value = def;
    } else if (formatText) {
        value = [NSString stringWithFormat:formatText,value];
    }
    
    NSDictionary *valueDic = [NSDictionary dictionaryWithObject:value forKey:tokey];
    [array addObject:valueDic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0){
        AutoTextTableViewCell *cell = [[AutoTextTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AutoTextTableViewCell"];
        [cell setDataArray:self.messageData];
        return cell;
        
    } else if(indexPath.section == 1){
        static NSString *CellIdentifier  = @"OAMessageDetailViewController";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            UITextView *label = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 200)];
            label.font = [UIFont systemFontOfSize:15.0];
            label.textColor = [UIColor darkGrayColor];
            label.text = self.messageContent;
            label.textAlignment = NSTextAlignmentLeft;
            label.userInteractionEnabled = NO;
            [cell.contentView addSubview:label];
        }
        return cell;
    }
    
    return nil;
}

#pragma mark
#pragma mark - UITableViewDelegate

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //基本信息不添加
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 30)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, SuperWidth -10, 29.5)];
    lable.textColor = [UIColor darkGrayColor];
    lable.font = [UIFont boldSystemFontOfSize:15];
    
    if(section == 0) {
        lable.text = @"消息明细";
    } else {
        lable.text = @"消息内容";
    }
    [view addSubview:lable];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 29, SuperWidth, 1)];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [view addSubview:lineView];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        AutoTextTableViewCell *cell = [[AutoTextTableViewCell alloc] init];
        [cell setDataArray:self.messageData];
        return cell.contentView.frame.size.height;
    } else {
        return 200;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

@end
